package database;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

import java.security.spec.ECField;
import java.util.ArrayList;
import java.util.List;

import entity.BillInfo;

public class BillDBHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "bill.db";
    private static final String BILL_INFO = "bill_info";
    private static final int DB_VERSION = 1;
    private static BillDBHelper mHelper = null;
    private SQLiteDatabase mRDB = null;
    private SQLiteDatabase mWDB = null;
    private BillDBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    public static BillDBHelper getInstance(Context context){
        if (mHelper == null){
            mHelper = new BillDBHelper(context);
        }
        return mHelper;
    }

    public SQLiteDatabase openReadLink(){
        if (mRDB == null){
            mRDB = mHelper.getReadableDatabase();
        }
        return mRDB;
    }

    public SQLiteDatabase openWriteLink(){
        if (mWDB == null){
            mWDB = mHelper.getWritableDatabase();
        }
        return mWDB;
    }
    public void closeLink(){
        if (mWDB != null){
            mWDB.close();
        }
        if (mRDB != null){
            mRDB.close();
        }
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "create table " + BILL_INFO +
                "(" + " id integer primary key autoincrement not null," +
        "date varchar not null, "   +
        "type integer not null," +
        "amount double not null, " +
                " remark varchar not null"+
                ");" ;

        db.execSQL(sql);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
    public void insertBillInfo(List<BillInfo> list){
        try {
            mWDB.beginTransaction();
            for (BillInfo info : list) {
                ContentValues values = new ContentValues();
                values.put("date",info.date);
                values.put("type",info.type);
                values.put("amount",info.amount );
                values.put("remark",info.remark);
                mWDB.insert(BILL_INFO,null,values);
            }
            mWDB.setTransactionSuccessful();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            mWDB.close();
        }
    }

    @SuppressLint("Range")
    public List<BillInfo> queryAllBillInfo(String yearMonth){
        String sql = "select * from "+ BILL_INFO + " where date like '" + yearMonth + "%'";
        List<BillInfo> infos = new ArrayList<>();
        Cursor cursor = mRDB.rawQuery(sql, null);
        while (cursor.moveToNext()){
            BillInfo info = new BillInfo();
            info.id = cursor.getInt(cursor.getColumnIndex("id"));
            info.date = cursor.getString(cursor.getColumnIndex("date"));
            info.type = cursor.getInt(cursor.getColumnIndex("type"));
            info.amount = cursor.getDouble(cursor.getColumnIndex("amount"));
            info.remark = cursor.getString(cursor.getColumnIndex("remark"));
            infos.add(info);
        }
        cursor.close();
        return infos;
    }
    public void save(BillInfo info){
                ContentValues values = new ContentValues();
                values.put("date",info.date);
                values.put("type",info.type);
                values.put("amount",info.amount );
                values.put("remark",info.remark);
                Log.e("TAG", "save: "+info);
                mWDB.insert(BILL_INFO,null,values);
    }
}
